﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;

namespace ASPItalia.ModelVirtualCasting.Web.Tests.Utils
{
    public static class Helpers
    {
        public static MemberInfo GetMemberInfo(Expression<Func<object>> expression)
        {
            return GetMemberInfoInternal(expression);
        }

        public static MemberInfo GetMemberInfo<T>(Expression<Func<T, object>> expression)
        {
            return GetMemberInfoInternal(expression);
        }

        private static MemberInfo GetMemberInfoInternal(LambdaExpression expression)
        {
            MemberExpression me = (MemberExpression)expression.Body;
            return me.Member;
        }

        public static string ExpressionToMemberName(Expression<Func<object>> expression)
        {
            return GetMemberInfoInternal(expression).Name;
        }

        public static string ExpressionToMemberName<T>(Expression<Func<T, object>> expression)
        {
            return GetMemberInfoInternal(expression).Name;
        }

        public static bool ContainsKey<T>(this Dictionary<string, string> dictionary, Expression<Func<T, object>> expression)
        {
            return dictionary.ContainsKey(ExpressionToMemberName(expression));
        }

        public static bool ContainsKey(this Dictionary<string, string> dictionary, Expression<Func<object>> expression)
        {
            return dictionary.ContainsKey(ExpressionToMemberName(expression));
        }
    }
}
